package com.lzw.DailyAudit.service.dealSourceData_01;


import cn.hutool.core.util.StrUtil;
import com.hooya.fa.eu.biz.DailyAudit.entity.CkDict;
import com.hooya.fa.eu.biz.DailyAudit.entity.dto.StoreCorpDTO;
import com.hooya.fa.eu.biz.DailyAudit.mapper.DailyCkDictMapper;
import com.hooya.fa.eu.biz.DailyAudit.mapper.DailyMetaStoreMapper;
import com.hooya.fa.eu.biz.DailyAudit.mapper.DailyXsjl2303Mapper;
import com.hooya.fa.eu.biz.DailyAudit.service.dateAggregation_02.CommonService;
import com.xxl.job.core.context.XxlJobHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class Pre_Test {

	@Autowired
	private DailyXsjl2303Mapper xsjl2303Mapper;

	@Autowired
	private DailyMetaStoreMapper metaStoreMapper;

	@Autowired
	private DailyCkDictMapper ckDictMapper;

	@Autowired
	private CommonService commonService;

	final List<String> toEmails = Collections.singletonList("apple.lan@fdsintl.com");

	public void preTest() {

		// 加载fa_eu.meta_store,得到所有的 店铺-公司 映射数据 （store-corp）
		List<StoreCorpDTO> storeCorpList = metaStoreMapper.getStoreAndCorp(); //fa_eu.meta_store
		Map<String, String> storeCorpDic = storeCorpList.stream().collect(Collectors.toMap(StoreCorpDTO::getStore, StoreCorpDTO::getCorp));
		log.info("成功加载了 {} 条店铺-公司映射数据", storeCorpDic.size());


		// 测试是否有新店铺
		List<String> stores = xsjl2303Mapper.getStores();  //rxh_europe.xsjl_2303 - dwd.dwd_ord_ordinfo
		// 将现有店铺转为小写存入集合  -  去除数据的前后空格
		List<String> existStoreLower = storeCorpDic.keySet().stream()
				.map(String::trim)
				.map(String::toLowerCase)
				.collect(Collectors.toList());
		log.info("已存在的店铺数量： {}", existStoreLower.size());

		//用于存储新店铺----不存在于existStoreLower中的store
		List<String> newStores = stores.stream()
				.filter(f -> StrUtil.isNotBlank(f) && !existStoreLower.contains(f.toLowerCase()))
				.collect(Collectors.toList());
		log.info("新的店铺数量： {}", newStores.size());

		if (!newStores.isEmpty()) {
			log.warn("新增店铺列表: {}", newStores);
			// 发送简单文本邮件只需要邮箱地址
			commonService.sendTextEmail("新增店铺列表", "有新增店铺列表: " + newStores + "麻烦添加一下", toEmails);
			XxlJobHelper.log("已发送邮件去询问新增店铺相关情况");
			throw new RuntimeException("新增店铺列表: " + newStores);
		} else {
			log.info("当天没有新增的店铺，测试新店铺通过");
			XxlJobHelper.log("当天没有新增的店铺，测试新店铺通过");
		}


		// 查询OEU订单
		List<String> oeuOrderIds = xsjl2303Mapper.getOeuIds(); //rxh_europe.xsjl_2303 - dwd.dwd_ord_ordinfo
		// 从数据库获取oeuOrderMap字典
		List<CkDict> oeuOrderDictList = ckDictMapper.selectByName("oeuOrderMap"); //fa_eu.ck_dict

		//构建映射关系
		List<String> oeuOrderDictParamKeys = oeuOrderDictList.stream()
				.map(CkDict::getParamKey)
				.collect(Collectors.toList());

		// 检查订单是否存在
		List<String> newOeuOrders = oeuOrderIds.stream()
				.filter(f -> StrUtil.isNotBlank(f) && !oeuOrderDictParamKeys.contains(f))
				.collect(Collectors.toList());

		if (!newOeuOrders.isEmpty()) {
			log.info("新增店铺订单信息集合: {}", newOeuOrders);
			commonService.sendTextEmail("新增oeu订单", "有新oeu订单: " + newOeuOrders  + "麻烦添加一下", toEmails);
			XxlJobHelper.log("已发送邮件去询问新增oeu订单相关情况");
			throw new RuntimeException("新增店铺订单信息集合: " + newOeuOrders);
		} else {
			log.info("当天没有oeu订单，测试OEU订单通过");
			XxlJobHelper.log("当天没有oeu订单，测试OEU订单通过");
		}

		log.info("preTest执行成功");
	}


}